package com.example.springinit2.test;

import lombok.Data;
import org.springframework.context.annotation.Primary;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class ParallelStream {

  public static void main(String[] args) {

	List<Person> phpProgrammers = new ArrayList<Person>() {
	  {
		add(new Person("Jarrod", "Pace", "PHP programmer", "male", 34, 1550));
		add(new Person("Clarette", "Cicely", "PHP programmer", "female", 23, 1200));
		add(new Person("Victor", "Channing", "PHP programmer", "male", 32, 1600));
		add(new Person("Tori", "Sheryl", "PHP programmer", "female", 21, 1000));
		add(new Person("Osborne", "Shad", "PHP programmer", "male", 32, 1100));
		add(new Person("Rosalind", "Layla", "PHP programmer", "female", 25, 1300));
		add(new Person("Fraser", "Hewie", "PHP programmer", "male", 36, 1100));
		add(new Person("Quinn", "Tamara", "PHP programmer", "female", 21, 1000));
		add(new Person("Alvin", "Lance", "PHP programmer", "male", 38, 1600));
		add(new Person("Evonne", "Shari", "PHP programmer", "female", 40, 1800));
	  }
	};
	System.out.println("计算付给 Java programmers 的所有money:");
	int totalSalary = phpProgrammers
			.parallelStream()
			.mapToInt(p -> {
			  //try {
				//TimeUnit.MICROSECONDS.sleep(200L);
			  //} catch (InterruptedException e) {
				//e.printStackTrace();
			  //}

			  System.out.println(System.currentTimeMillis()+"线程:?"+p.getFirstName());
			  return p.getSalary();})
			.sum();


	new Thread(() -> browseNews()).start();
  }

  private static void browseNews(){
	for (; ; ) {
	  System.out.println("browse news");
	  try {
		sleep(1);
	  } catch (InterruptedException e) {
		e.printStackTrace();
	  }
	}
  }
  private static void enjoyMusic(){
	for (; ; ) {
	  System.out.println("browse news");
	  try {
		sleep(1);
	  } catch (InterruptedException e) {
		e.printStackTrace();
	  }
	}
  }

}
